package SS;
import java.util.Scanner;
import java.util.Arrays;
public class AC {
		public static void main(String[] args) {
			Scanner sc = new Scanner(System.in);
			String a = sc.nextLine();
			int d = Integer.parseInt(a);
			String[] b = new String[d];
			for(int i=0;i<d;i++){
				b[i] = sc.nextLine();
			} 
			String c = sc.nextLine();
			switch (c) {
			case "sort1":
				String[] s=new String[d];
			
				for(int a1=0;a1<d;a1++){
				s[a1] = b[a1].substring(6,10)+b[a1].substring(10,12)+b[a1].substring(12,14);
				}
				int [] x = covertToIntDigits(s);
				Arrays.sort(x);
				for(int i = 0;i<d;i++){
			    System.out.println(x[i]);
				}
				break;
		
			case"sort2":
				String[] n=new String[d];
				for(int a1=0;a1<d;a1++){
				n[a1] = b[a1].substring(6,10)+b[a1].substring(10,12)+b[a1].substring(12,14);
				}
				int [] y = covertToIntDigits(n);
				int [] p = covertToIntDigits(b);
				Arrays.sort(y);
				for(int i = 0;i<d;i++){
					System.out.println(y[i]);
				}
				break;
				
			default:
				System.out.println("exit");
				
			}
		}
			
			
			




		private static int[] covertToIntDigits(String[] strDigits) {
			int[] array = new int[strDigits.length];
			for (int i = 0; i < strDigits.length; i++) {
				array[i] = Integer.parseInt(strDigits[i]);
			}
			return array;
		}

}
 